package com.os.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author Matt
 * @Date 2022-07-03
 * @Desc:
 * @Version V1.0
 */
public class ReflectService {

    /**
     * 服务方法
     * @param name 姓名
     */
    public void sayHello(String name){
        System.err.println("hello: " + name);
    }

    /**
     * 测试入口
     */
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //通过反射ReflectService对象
        Object service = Class.forName(ReflectService.class.getName()).newInstance();
        //获取服务方法，sayHello
        Method method = service.getClass().getMethod("sayHello", String.class);
        //反射调用方法
        method.invoke(service, "Matt");
    }
}
